<template>

  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCel">
    <!-- 多选框组 -->
    <el-checkbox-group v-model="roleIds">
      <!-- 要循环的选项 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <!-- 定义footer插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
// 获取角色
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  data() {
    return {

      list: [],
      roleIds: []
    }
  },
  created() {
    this.getRoleList()
  },
  props:{
    showRoleDialog: {
      default: false,
      type: Boolean
    },
    userId: {
      type: String,
      default: null,
      required:true
    }
  },
  // 用户的id 用来查询当前用户的角色信息
  methods: {
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds // 赋值本用户的角色
    },
    //  获取所有角色
    async getRoleList() {
      const { rows } = await getRoleList()
      this.list = rows
    },
    // 点击确定事件
    // 掉接口  关闭弹层
    async btnOk() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$emit('update:showRoleDialog', false)
      this.$message.success('分配角色成功')
    },
    // 点击取消事件
    // 清空数据 关闭弹层
    async btnCel() {
      this.roleIds = []
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style scoped></style>
